<?php
class Message_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function get($data)
    {
    	$this->db->select('message.*');
    	$this->db->from('message');
    	
    	if (isset($data['idmessage'])) {
    		$this->db->where('message.idmessage', $data['idmessage']);
    	}
    	
    	$this->db->order_by('message.date desc');
    	
    	return $this->db->get();
    }
    
	function get_member_messages($data)
    {
    	$this->db->select('message.*');
    	$this->db->from('message');
    	$this->db->where('message.idmemberfrom', $this->session->userdata['idmember']);
    	$this->db->or_where('message.idmemberto', $this->session->userdata['idmember']);
    	$this->db->order_by('message.date desc');
    	
    	if (!isset($data['limit']) || !is_numeric($data['limit']))
    	{
    		$data['limit'] = 5;
    	}
    	
    	if (!isset($data['offset']) || !is_numeric($data['offset']))
    	{
    		$data['offset'] = 0;
    	}
    	elseif ($data['offset'] > 0)
    	{
    		$data['offset'] = ($data['limit'] * $data['offset']);
    	}
    	
    	$this->db->limit($data['limit'], $data['offset']);
    	
    	return $this->db->get();
    }
    
	function get_count_messages()
    {
    	$this->db->select('count(message.id) AS count');
    	$this->db->from('message');
    	$this->db->where('message.idmemberfrom', $this->session->userdata['idmember']);
    	$this->db->or_where('message.idmemberto', $this->session->userdata['idmember']);
    	return $this->db->get()->row()->count;
    }
    
	function insert($data)
    {
    	$this->db->trans_begin();
    	
    	$data_insert = array(
		   'idmemberfrom' => $data['idmemberfrom'],
		   'idmemberto' => $data['idmemberto'],
		   'message' => $data['message'],
		   'idtype' => $data['idtype'],
    	   'idmemberfromtype' => $data['idmemberfromtype'],
		   'idmembertotype' => $data['idmembertotype'],
		);
		
		if (isset($data['idsubscription']))
		{
			$data_insert['idsubscription'] = $data['idsubscription'];
		}
		
    	if (isset($data['idjob']))
		{
			$data_insert['idjob'] = $data['idjob'];
		}
		
    	if (isset($data['idmessageabove']))
		{
			$data_insert['idmessageabove'] = $data['idmessageabove'];
		}
		
		$this->db->set('date', 'NOW()', false);
		$this->db->insert('message', $data_insert);
		
    	if ($this->db->trans_status() === false)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    return true;
		}
    }
    
	function update($data)
    {
    	$this->db->trans_begin();
    	
    	$data_insert = array(
		   'idmemberfrom' => $data['idmemberfrom'],
		   'idmemberto' => $data['idmemberto'],
		   'message' => $data['message'],
    		'idtype' => $data['idtype']
		);
		
		$this->db->where('id', $data['idmessage']);
		$this->db->update('job', $data_insert);
		
    	if ($this->db->trans_status() === false)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    return true;
		}
    }
    
	function delete($data)
    {
    	$this->db->trans_begin();
		
		$this->db->delete('message', array('id' => $data['id']));
		
    	if ($this->db->trans_status() === false)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    return true;
		}
    }
}
?>